//
//  HZSummaryView.m
//  HZTC_Stock
//
//  Created by MayerF on 17/3/13.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZSummaryView.h"

@interface HZSummaryView()
@property (nonatomic, strong)UILabel *pCurrentPrice;
@property (nonatomic, strong)UILabel *pZhangDie;
@property (nonatomic, strong)UILabel *pZhangFu;
@property (nonatomic, strong)UILabel *pMaxPrice;
@property (nonatomic, strong)UILabel *pMaxPriceContent;
@property (nonatomic, strong)UILabel *pMinPrice;
@property (nonatomic, strong)UILabel *pMinPriceContent;
@property (nonatomic, strong)UILabel *pOpenPrice;
@property (nonatomic, strong)UILabel *pOpenPriceContent;
@property (nonatomic, strong)UILabel *pLastPrice;
@property (nonatomic, strong)UILabel *pLastPriceContent;
@property (nonatomic, strong)UILabel *pHuanShouLv;
@property (nonatomic, strong)UILabel *pHuaShouLvContent;
@property (nonatomic, strong)UILabel *pLiangBi;
@property (nonatomic, strong)UILabel *pLiangBiContent;
@property (nonatomic, strong)UIView  *pLine;
@property (nonatomic, strong)UIView  *pBottomLine;

@property (nonatomic, strong)UIButton      *pCancelBtn;
@property (nonatomic, strong)HZOwnedStockModel *pModel;
@property (nonatomic, copy)HZMinPriceModel *pMinModel;
@property (nonatomic, copy)HZMinPriceModel *pKLineMinModel;

@property (nonatomic, assign)BOOL isLoadData;//是否加载数据

@end

@implementation HZSummaryView

#pragma mark -
#pragma mark 生命周期
- (instancetype)init{
    self = [super init];
    if (self) {
        [self p_commonInit];
        self.backgroundColor = [HZCommonTool getThemeColor];
    }
    return self;
}
- (void)p_commonInit
{
    _pCurrentPrice = [UILabel new];
    _pZhangDie = [UILabel new];
    _pZhangFu = [UILabel new];
    _pMaxPrice = [UILabel new];
    _pMaxPriceContent = [UILabel new];
    _pMinPrice = [UILabel new];
    _pMinPriceContent = [UILabel new];
    _pOpenPrice = [UILabel new];
    _pOpenPriceContent = [UILabel new];
    _pLastPrice = [UILabel new];
    _pLastPriceContent = [UILabel new];
    _pHuanShouLv = [UILabel new];
    _pHuaShouLvContent = [UILabel new];
    _pLiangBi = [UILabel new];
    _pLiangBiContent = [UILabel new];
    _pLine = [UIView new];
    _pBottomLine = [UIView new];
    
    _pCurrentPrice.textAlignment = NSTextAlignmentCenter;
    _pZhangDie.textAlignment = NSTextAlignmentCenter;
    _pZhangFu.textAlignment = NSTextAlignmentCenter;
    _pCurrentPrice.font = [UIFont systemFontOfSize:30];
    _pMaxPriceContent.font = _pOpenPriceContent.font = _pMinPriceContent.font = _pLastPriceContent.font = _pLiangBiContent.font = _pHuaShouLvContent.font = [UIFont systemFontOfSize:15];
    
    _pMaxPrice.textAlignment = _pOpenPrice.textAlignment = _pHuanShouLv.textAlignment = _pMinPrice.textAlignment = _pLastPrice.textAlignment = _pLiangBi.textAlignment = NSTextAlignmentCenter;
    _pMaxPriceContent.textAlignment = _pOpenPriceContent.textAlignment = _pHuaShouLvContent.textAlignment = _pMinPriceContent.textAlignment = _pLastPriceContent.textAlignment =
    _pLiangBiContent.textAlignment = NSTextAlignmentCenter;
    
    _pMaxPriceContent.adjustsFontSizeToFitWidth = YES;
    _pOpenPriceContent.adjustsFontSizeToFitWidth = YES;
    _pMinPriceContent.adjustsFontSizeToFitWidth = YES;
    _pLastPriceContent.adjustsFontSizeToFitWidth = YES;
    _pHuaShouLvContent.adjustsFontSizeToFitWidth = YES;
    _pLiangBiContent.adjustsFontSizeToFitWidth = YES;
    _pCurrentPrice.adjustsFontSizeToFitWidth = YES;
    _pZhangDie.adjustsFontSizeToFitWidth = YES;
    _pZhangFu.adjustsFontSizeToFitWidth = YES;
    
    [self addSubview:_pCurrentPrice];
    [self addSubview:_pZhangDie];
    [self addSubview:_pZhangFu];
    [self addSubview:_pMaxPrice];
    [self addSubview:_pMaxPriceContent];
    [self addSubview:_pMinPrice];
    [self addSubview:_pMinPriceContent];
    [self addSubview:_pOpenPrice];
    [self addSubview:_pOpenPriceContent];
    [self addSubview:_pLastPrice];
    [self addSubview:_pLastPriceContent];
    [self addSubview:_pHuanShouLv];
    [self addSubview:_pHuaShouLvContent];
    [self addSubview:_pLiangBi];
    [self addSubview:_pLiangBiContent];
    [self addSubview:_pLine];
    [self addSubview:_pBottomLine];
    
    _pHuanShouLv.font = _pLiangBi.font = [UIFont systemFontOfSize:15];
    _pLastPrice.font = _pOpenPrice.font = _pMaxPrice.font = _pMinPrice.font = [UIFont systemFontOfSize:13];

    _pMaxPrice.textColor = _pMinPrice.textColor = _pOpenPrice.textColor =
    _pLastPrice.textColor = _pLiangBi.textColor = _pHuanShouLv.textColor = kMYHexToRGB(0x888888, 1);
    _pLine.backgroundColor = _pBottomLine.backgroundColor = [HZCommonTool getThemeSeperateColorWithDefaultColor:nil];
    
    //添加通知监听者
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(radioGroupClick:) name:kRadioGroupClickNotification object:nil];
    
    [self p_updateConstraintsWithCodeInfo:nil];
    [self p_updateDefaultInfoWithCodeInfo:nil];
}
#pragma mark -
#pragma mark public
- (void)bindCodeInfo:(HZOwnedStockModel *)codeInfo
{
    if (_isLoadData) { return;}
    _pModel = codeInfo;
    [self p_updateConstraintsWithCodeInfo:codeInfo];
    [self p_updateDefaultInfoWithCodeInfo:codeInfo];
}

- (void)updateNewData:(HZMinPriceModel *)data from:(KFromType)from
{
    if (from == KFromMinType) {
        _pMinModel = data;
    }else if (from == KFromKLineType) {
        _pKLineMinModel = data;
    }
    _isLoadData = YES;
    
    if (_isShowTenLine) {
        return;
    }
    
    if (_pModel.nModel_Style == 1) {
        _pCurrentPrice.text = data.model_currentPrice;
        _pZhangDie.text = data.model_zhangDie;
        _pZhangFu.text = data.model_zhangFu;
        _pMaxPriceContent.text = data.model_maxPrice;
        _pMinPriceContent.text = data.model_minPrice;
        _pOpenPriceContent.text = data.model_openPrice;
        _pLastPriceContent.text = data.model_chengJiaoE;
        CGFloat fPrePrice = [data.model_prePrice floatValue];
        _pCurrentPrice.textColor = _pZhangDie.textColor = _pZhangFu.textColor = kMYGetZhangDieColor([data.model_currentPrice floatValue], fPrePrice);
        _pMaxPriceContent.textColor = kMYGetZhangDieColor([data.model_maxPrice floatValue], fPrePrice);
        _pOpenPriceContent.textColor = kMYGetZhangDieColor([data.model_openPrice floatValue], fPrePrice);
        _pMinPriceContent.textColor = kMYGetZhangDieColor([data.model_minPrice floatValue], fPrePrice);
    }else {
        _pCurrentPrice.text = data.model_currentPrice;
        _pZhangDie.text = data.model_zhangDie;
        _pZhangFu.text = data.model_zhangFu;
        _pMaxPriceContent.text = data.model_maxPrice;
        _pMinPriceContent.text = data.model_minPrice;
        _pOpenPriceContent.text = data.model_openPrice;
        _pLastPriceContent.text = data.model_prePrice;
        _pHuaShouLvContent.text = data.model_chengJiaoLiang;
        _pLiangBiContent.text = data.model_chengJiaoE;
        CGFloat fPrePrice = [data.model_prePrice floatValue];
        _pCurrentPrice.textColor = _pZhangDie.textColor = _pZhangFu.textColor = [HZCommonTool getThemeZhangDieColorWithValue1:[data.model_currentPrice floatValue] value2:fPrePrice];
        _pMaxPriceContent.textColor = [HZCommonTool getThemeZhangDieColorWithValue1:[data.model_maxPrice floatValue] value2:fPrePrice];
        _pMinPriceContent.textColor = [HZCommonTool getThemeZhangDieColorWithValue1:[data.model_minPrice floatValue] value2:fPrePrice];
    }
    [self updateThemeColor];
}
#pragma mark -
#pragma mark private
- (void)p_updateConstraintsWithCodeInfo:(HZOwnedStockModel *)codeInfo
{
    [_pCurrentPrice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.left.mas_equalTo(self).offset(10);
        make.bottom.mas_equalTo(_pLine.mas_top);
        make.width.mas_equalTo(self).multipliedBy(0.3f);
    }];
    [_pLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.bottom.mas_equalTo(_pLastPrice.mas_top);
        make.height.mas_equalTo(0.5f);
    }];
    [_pZhangDie mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_pCurrentPrice.mas_right);
        make.bottom.mas_equalTo(_pZhangFu.mas_top);
        make.width.mas_equalTo(self).multipliedBy(0.23f);
        make.top.mas_equalTo(self);
        make.right.mas_equalTo(_pHuanShouLv.mas_left);
    }];
    [_pZhangFu mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_pZhangDie);
        make.bottom.mas_equalTo(_pCurrentPrice);
        make.width.mas_equalTo(_pZhangDie);
        make.right.mas_equalTo(_pZhangDie);
        make.height.mas_equalTo(_pZhangDie);
    }];
    [_pHuanShouLv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.right.mas_equalTo(_pHuaShouLvContent.mas_left);
        make.bottom.mas_equalTo(_pZhangDie);
        make.width.mas_equalTo(_pZhangDie);
    }];
    [_pHuaShouLvContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.right.mas_equalTo(self).offset(-10);
        make.bottom.mas_equalTo(_pHuanShouLv);
    }];
    [_pLiangBi mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pHuanShouLv.mas_bottom);
        make.left.right.mas_equalTo(_pHuanShouLv);
        make.bottom.mas_equalTo(_pZhangFu);
        make.width.mas_equalTo(_pHuanShouLv);
    }];
    [_pLiangBiContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pLiangBi);
        make.left.mas_equalTo(_pLiangBi.mas_right);
        make.right.mas_equalTo(self).offset(-10);
        make.bottom.mas_equalTo(_pLiangBi);
    }];
    
    
    [_pLastPrice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pLine.mas_bottom);
        make.left.mas_equalTo(self).offset(10);
        make.bottom.mas_equalTo(self);
        make.height.mas_equalTo(35);
        make.width.mas_equalTo(30);
    }];
    [_pLastPriceContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pLastPrice);
        make.left.mas_equalTo(_pLastPrice.mas_right);
        make.height.mas_equalTo(_pLastPrice);
    }];
    [_pOpenPrice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pLastPrice);
        make.left.mas_equalTo(_pLastPriceContent.mas_right).offset(5);
        make.bottom.mas_equalTo(_pLastPrice);
    }];
    [_pOpenPriceContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pLastPrice);
        make.left.mas_equalTo(_pOpenPrice.mas_right);
        make.bottom.mas_equalTo(_pLastPrice);
    }];
    [_pMaxPrice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pLastPrice);
        make.left.mas_equalTo(_pOpenPriceContent.mas_right).offset(5);
        make.bottom.mas_equalTo(_pLastPrice);
    }];
    [_pMaxPriceContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pLastPrice);
        make.left.mas_equalTo(_pMaxPrice.mas_right);
        make.bottom.mas_equalTo(_pLastPrice);
    }];
    [_pMinPrice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pLastPrice);
        make.left.mas_equalTo(_pMaxPriceContent.mas_right).offset(5);
        make.bottom.mas_equalTo(_pLastPrice);
        make.width.mas_equalTo(@[_pLastPrice,_pOpenPrice, _pMaxPrice]);
    }];
    [_pMinPriceContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pLastPrice);
        make.left.mas_equalTo(_pMinPrice.mas_right);
        make.right.mas_equalTo(self).offset(-10);
        make.bottom.mas_equalTo(_pLastPrice);
        make.width.mas_equalTo(@[_pLastPriceContent, _pOpenPriceContent, _pMaxPriceContent]);
    }];
    
    [_pBottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self);
        make.height.mas_equalTo(0.5f);
    }];
}

- (void)p_updateDefaultInfoWithCodeInfo:(HZOwnedStockModel *)codeInfo
{
    if (codeInfo.nModel_Style == 1) {
        _pCurrentPrice.text = _pZhangDie.text = _pZhangFu.text = kDefaultContent;
        _pMaxPriceContent.text = _pMinPriceContent.text = _pOpenPriceContent.text = _pLastPriceContent.text = kDefaultContent;
        _pMaxPrice.text = kHZMaxPrice;
        _pMinPrice.text = kHZMinPrice;
        _pOpenPrice.text = kHZOpenPrice;
        _pLastPrice.text = kHZE;
    }else {
        _pCurrentPrice.text = _pZhangDie.text = _pZhangFu.text = kDefaultContent;
        _pMaxPriceContent.text = _pMinPriceContent.text = _pOpenPriceContent.text = _pLastPriceContent.text = _pHuaShouLvContent.text = _pLiangBiContent.text = kDefaultContent;
        _pMaxPrice.text = kHZMaxPrice;
        _pMinPrice.text = kHZMinPrice;
        _pOpenPrice.text = kHZOpenPrice;
        _pLastPrice.text = kHZLastPrice;
        _pHuanShouLv.text = kHZChengJiaoLiang;
        _pLiangBi.text = kHZChengJiaoE;
    }
}

- (UIView *)p_loadCancelBtn
{
    if (!_pCancelBtn) {
        _pCancelBtn = [UIButton new];
        [_pCancelBtn setImage:[UIImage imageNamed:@"off"] forState:UIControlStateNormal];
        [_pCancelBtn addTarget:self action:@selector(cancleBtnOnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_pCancelBtn];
    }
    [_pCancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.right.mas_equalTo(self).offset(-10);
        make.bottom.mas_equalTo(self);
        make.width.mas_equalTo(self.mas_height);
    }];
    return _pCancelBtn;
}

- (void)cancleBtnOnClick:(UIButton *)btn
{
    [self.parent upToParentWithObjc:self withType:kHZ_cancelLandscapeScreen_Action withParamter:nil];
}

- (void)p_updateNewConstraints:(kHZSingleStockState)state
{
    if (state == kHZSingleStock_landscape_State) {
        [self p_loadCancelBtn].hidden = NO;
    }else{
        _pCancelBtn.hidden = YES;
    }
}

- (void)radioGroupClick:(NSNotification *)noti
{
    UIButton *btn = noti.object;
    if ([btn.currentTitle isEqual:kHZMinute]) {
        return;
    }
//    if ([btn.currentTitle isEqual:kHZFenShi] || [btn.currentTitle isEqual:kHZMingXi]) {
//        _nState = 0;
//        [self updateNewData:_pMinModel from:KFromMinType];
//    }else {
//        _nState = 1;
//        [self updateNewData:_pKLineMinModel from:KFromKLineType];
//    }
    [self p_updateConstraintsWithCodeInfo:_pModel];
}
#pragma mark -
- (void)upToParentWithObjc:(id)objc withType:(NSInteger)type withParamter:(id)paramter{
}
- (void)downToChildWithObjc:(id)objc withType:(NSInteger)type withParamter:(id)paramter{
    switch (type) {
        case kHZ_updateConstraints_Action:
        {
            if ([paramter isKindOfClass:[NSNumber class]]) {
                NSNumber *pNumState = (NSNumber *)paramter;
                [self p_updateNewConstraints:[pNumState integerValue]];
            }
        }
            break;
            
        default:
            break;
    }
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)updateThemeColor
{
    _pLiangBiContent.textColor = _pHuaShouLvContent.textColor = [HZCommonTool getThemeZhangDieColor];
    _pLastPriceContent.textColor = _pOpenPriceContent.textColor = [HZCommonTool getThemeZhangDieColor];
}

@end
